package com.lyj.hw_student.system.controller;

import com.lyj.hw_student.system.entity.Student;
import com.lyj.hw_student.system.service.IStudentService;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private IStudentService studentService;

    /**
     * 添加学生
     */
    @PostMapping("/add")
    public Map<String, Object> addStudent(@RequestBody Student student) {
        Map<String, Object> result = new HashMap<>();
        boolean success = studentService.save(student);
        result.put("success", success);
        result.put("message", success ? "添加成功" : "添加失败");
        return result;
    }

    /**
     * 删除学生
     */
    @DeleteMapping("/delete/{studentNo}")
    public Map<String, Object> deleteStudent(@PathVariable Integer studentNo) {
        Map<String, Object> result = new HashMap<>();
        boolean success = studentService.removeById(studentNo);
        result.put("success", success);
        result.put("message", success ? "删除成功" : "删除失败");
        return result;
    }

    /**
     * 修改学生
     */
    @PutMapping("/update")
    public Map<String, Object> updateStudent(@RequestBody Student student) {
        Map<String, Object> result = new HashMap<>();
        boolean success = studentService.updateById(student);
        result.put("success", success);
        result.put("message", success ? "修改成功" : "修改失败");
        return result;
    }

    /**
     * 查询学生
     */
    @GetMapping("/get/{studentNo}")
    public Student getStudent(@PathVariable Integer studentNo) {
        return studentService.getById(studentNo);
    }

    /**
     * 查询所有学生
     */
    @GetMapping("/list")
    public List<Student> listStudents() {
        return studentService.list();
    }
}